package topo.action;

import com.zhtelecom.common.topograph.TopoGraphView;
import com.zhtelecom.common.topograph.TopoNode;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import topo.MainFrame;
import topo.data.NodeDevice;
import topo.data.NodeLink;
import topo.service.TopoDataManager;
import topo.utils.DialogTools;

public class SetLinkEndAction extends AbstractAction {
    public void actionPerformed(ActionEvent e) {
        try {
            TopoGraphView topoView = (TopoGraphView) getValue("ActionDataTopoview");
            topoView.setLinkTarget(topoView.getSelectedNode());
            if ((topoView.getLinkSource() != null) && (topoView.getLinkTarget() != null)) {
                NodeLink dblink = new NodeLink();
                NodeDevice nodeA = (NodeDevice) topoView.getLinkSource().getUserID();
                NodeDevice nodeZ = (NodeDevice) topoView.getLinkTarget().getUserID();

                dblink.setFromNode(nodeA.getNodeName());
                dblink.setToNode(nodeZ.getNodeName());
                dblink.setLinkName(dblink.genKey());

                TopoDataManager.getInstance().addLink(dblink);
            } else {
                DialogTools.showErroMsg(MainFrame.mainFrame, "请先选择连接起点，再选择终点！");
                return;
            }
        } catch (Exception ex) {
            DialogTools.showErroMsg(MainFrame.mainFrame, "创建链接出错!");
            ex.printStackTrace();
        }
    }

    public SetLinkEndAction() {
        super("设置连接终点");
    }
}